<?php
class GuestbookController extends AppController {

	public $name = 'guestbook';

	function beforeFilter() {
	    parent::beforeFilter(); 
		$this->C_auth->autoRedirect = false; //For override login
	    $this->C_auth->allowedActions = array(/* 'index','userdel','shopdel','shoplist','doguestbook','sellerreply' */);

	}
	
	public function doguestbook() {
			
        $user_id = $this->my->id;

        $post['name'] = Request::get_args('name');
        $post['email'] = Request::get_args('email');
        $post['contact'] = Request::get_args('contact');
        $post['content'] = Request::get_args('content');
        $post['shop_id'] = intval(Request::get_args('shop_id'));
        $post['shop_name'] = Request::get_args('shop_name');
        $post['add_time'] = date("Y-m-d H:i:s");
        $post['add_ip'] = $_SERVER['REMOTE_ADDR'];
        $post['user_id'] = intval($user_id);
/*         $post['group_id'] = intval(Request::get_args('group_id'));
        $post['group_name'] = Request::get_args('group_name');*/
        $post['goods_id'] = intval(Request::get_args('goods_id')); 
        if (!$post['goods_id']) {
	        $post['goods_id']=0;
        }

		if ( md5(strtolower($this->data['veriCode'])) != $this->C_session->read('captcha') ) {
            $this->flash("验证码错误!", Router::url('shop/index/'.$post['shop_id']));
		} else {
			if (!$user_id){
				$this->flash("您还未登入，请先登入后再留言!", Router::url('user/login/'));
			} else{
				if($post['shop_id'] == $this->my->id) {
					 $this->flash("自己不能给自己留言!", Router::url('shop/index/'.$post['shop_id']));
				}else {		
					$shop_id = $post['shop_id'];
					$query = array(
							'fields' => array('shopinfo.*'),
							'conditions' => array('shopinfo.id' => $shop_id),
							);	
					$SHOP = POP::exe('[@@:UM shopinfo][@@:QU find:first]',$query);

					if(!$SHOP) { trigger_error("没有此商铺！");}//没有此商铺
					$SHOP = $SHOP['shopinfo'];		
					if($SHOP['lock_flg']) { trigger_error("商品不存在或商铺已锁定");}//锁定
					if($user_id!=$SHOP['user_id']){
						if($SHOP['open_flg']) {
							trigger_error("此商铺已关闭");
						}//关闭
					}		

					if($this->my->locked==1){
						$this->C_session->destroy();
						exit("您已经被锁定了");//非法操作
					}		
					$safedata['shopguest'] = $post;				
					if( POP::exe('[@@:UM shopguest][@@:QU save]',$safedata)) {	    
						$this->flash("留言成功!", Router::url('shop/index/'.$shop_id));
					} else {
						$this->flash("留言失败!", Router::url('shop/index/'.$shop_id));		
					}
				}
			}
		}
    }	
	
	public function userdel() {	
        $user_id = $this->my->id;	
        if($this->my->locked==1){
	        $this->C_session->destroy();
	        exit("您已经被锁定了");//非法操作
        }		
		
        if(empty($_POST)){
	        $gid = intval(Request::get_args('id'));
        }else{
	        $gid=Request::get_args('gid');
	        if (!$gid){
		        $this->flash("请选择删除选项", Router::url('guestbook/index/'));
	        }
	        $gid=implode(',',$gid);
        }
		$query = array(
				'fields' => array('shopguest.*'),
				'conditions' => array('shopguest.id' => $gid),
				);	
		$row = POP::exe('[@@:UM shopguest][@@:QU find:first]',$query);		
        if($row['0']['s']['user_id']!=$user_id){	        
		    $this->flash("您不能删除该内容，您没有这个权利！", Router::url('guestbook/index/'));			
        }
		
		$query  = array();		
		$query['keyid'] = true;
		$safeData['shopguest'] = array('user_del_status'=>0);				
		$query['set'] = $safeData;	
		$query['conditions'] = array('shopguest.id'=>$gid,'shopguest.user_id'=>$user_id);		
		if(POP::exe('[@@:UM shopguest][@@:QU save]',$query)) {	
	        $this->flash("删除成功！", Router::url('guestbook/index/'));
        } else {
	        $this->flash("删除失败!", Router::url('guestbook/index/'));
        }			
	}
	
	public function shopdel() {	
        $this->_teamRights();	
        $user_id = $this->my->id;	
        if($this->my->locked==1){
	        $this->C_session->destroy();
	        exit("您已经被锁定了");//非法操作
        }		
		
        if(empty($_POST)){
	        $gid = intval(Request::get_args('id'));
        }else{
	        $gid=Request::get_args('guest');
	        if (!$gid){
		        $this->flash("请选择删除选项", Router::url('guestbook/shoplist/'));
	        }
	        $gid=implode(',',$gid);
        }

		$query = array(
				'fields' => array('shopguest.*'),
				'conditions' => array('shopguest.id' => $gid),
				);	
		$row = POP::exe('[@@:UM shopguest][@@:QU find:first]',$query);		

        if($row['0']['s']['user_id']!=$user_id){	        
		    $this->flash("您不能删除该内容，您没有这个权利！", Router::url('guestbook/shoplist/'));			
        }
		
		$query  = array();		
		$query['keyid'] = true;
		$safeData['shopguest'] = array('shop_del_status'=>0);				
		$query['set'] = $safeData;	
		$query['conditions'] = array('shopguest.id'=>$gid,'shopguest.shop_id'=>$user_id);		
		if(POP::exe('[@@:UM shopguest][@@:QU save]',$query)) {		
	        $this->flash("删除成功！", Router::url('guestbook/shoplist/'));
        } else {
	        $this->flash("删除失败!", Router::url('guestbook/shoplist/'));
        }			
	}	

	public function sellerreply() {	
        $this->_teamRights();	
        $gid=intval(Request::get_args('id'));
        $reply = Request::get_args('reply');	
        $shop_id = $this->my->id;		
        if($reply) {
		    $safedata['shopguest']['reply'] = $reply;
		    $safedata['shopguest']['read_status'] =1 ;

			$query  = array();					
			$query['keyid'] = $gid;				
			$query['set'] = $safedata;							
            if(POP::exe('[@@:UM shopguest][@@:QU save]',$query) ) {	    
	            $this->flash("执行成功!", Router::url('guestbook/shoplist/'));
	        } else {
	            $this->flash("执行失败!", Router::url('shop/sellerreply/?id='.$gid));		
		    }		
		}else {		
			$query = array(
					'fields' => array('shopguest.*'),
					'conditions' => array('shopguest.shop_id' => $shop_id,'shopguest.id' => $gid,'shopguest.shop_del_status' => 1),
					);	
			$result = POP::exe('[@@:UM shopguest][@@:QU find:first]',$query);				
			
		    $result = $result['shopguest'];
            $this->set('result',$result);	
        }		
	}
	
	public function index() {
		if(!isset(Request::$query['page']))Request::$query['page']=1;
        $page = intval(Request::$query['page']);		
        $user_id = $this->my->id;
		
		$conditions = array();
		$conditions['shopguest.user_id'] = $user_id;
		$conditions['shopguest.user_del_status'] = 1;		
		$url_this = 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
		$tb = array('model' => 'shopguest',
					'fields' => array('shopguest.*'),
					'conditions' => $conditions,
					'order' => 'shopguest.add_time desc ',
					'limit' => '10',
					'group' => '',
					'url' => preg_replace("/&page=([^&]+)/","",$url_this),
					'page' => $page,
					'plist' => 6				
				); 
		$result = $this->fpage($tb); 					
        $this->set('result',$result);		
	}
	
	public function shoplist() {
        $this->_teamRights();	
	    $state = intval(Request::get_args('state'));
        $shop_id = $this->my->id;	
		if(!isset(Request::$query['page']))Request::$query['page']=1;
        $page = intval(Request::$query['page']);		
        $user_id = $this->my->id;
		$query = array(
				'fields' => array('shopinfo.*'),
				'conditions' => array('shopinfo.id' => $shop_id),
				);	
		$SHOP = POP::exe('[@@:UM shopinfo][@@:QU find:first]',$query);

        if(!$SHOP) { trigger_error("没有此商铺！");}//没有此商铺
		$SHOP = $SHOP['shopinfo'];		
        if($SHOP['lock_flg']) { trigger_error("商品不存在或商铺已锁定");}//锁定
        if($user_id!=$SHOP['user_id']){
	        if($SHOP['open_flg']) {
		        trigger_error("此商铺已关闭");
	        }//关闭
        }		

        if($this->my->locked==1){
	        $this->C_session->destroy();
	        exit("您已经被锁定了");//非法操作
        }
		$conditions = array();
	    if(!empty($state)){
		    if($state==1){
			    $conditions['shopguest.read_status'] = 0;
		    }
	    }
		$conditions['shopguest.shop_id'] = $shop_id;
		$conditions['shopguest.shop_del_status'] = 1;		
		$url_this = 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
		$tb = array('model' => 'shopguest',
					'fields' => array('shopguest.*'),
					'conditions' => $conditions,
					'order' => 'shopguest.add_time desc ',
					'limit' => '10',
					'group' => '',
					'url' => preg_replace("/&page=([^&]+)/","",$url_this),
					'page' => $page,
					'plist' => 6				
				); 
		$result = $this->fpage($tb); 		
        $this->set('result',$result);			

    }
	

}

?>